package com.lipuwater.agent.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ismartlib.utils.DisplayUtil;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseLayoutActivity;
import com.lipuwater.agent.http.bean.mall.OrderDesInfoBean;
import com.lipuwater.agent.http.bean.mall.OrderInfoBean;
import com.lipuwater.agent.ui.activity.sales.SalesActivity;
import com.lipuwater.agent.ui.utils.MallActivityList;
import com.lipuwater.agent.utils.DateUtils;
import com.lipuwater.agent.utils.LoaderImage;
import com.lipuwater.agent.utils.onclick.NoDoubleClick;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.github.xudaojie.qrcodelib.BitmapUtil;

public class OrderDesActivity extends BaseLayoutActivity {

    @Bind(R.id.text_buy_date)
    TextView textBuyDate;
    @Bind(R.id.text_client_name)
    TextView textClientName;
    @Bind(R.id.text_client_phone)
    TextView textClientPhone;
    @Bind(R.id.text_user_name)
    TextView textUserName;
    @Bind(R.id.text_user_phone)
    TextView textUserPhone;
    @Bind(R.id.text_address)
    TextView textAddress;
    @Bind(R.id.image_qrcode)
    ImageView imageQrcode;
    @Bind(R.id.image_devide_img)
    ImageView imageDevideImg;
    @Bind(R.id.text_device_name)
    TextView textDeviceName;
    @Bind(R.id.text_device_normsid)
    TextView textDeviceNormsid;
    private NoDoubleClick mNoDoubleClick = new NoDoubleClick();
    private String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_order_des);
        viewToolbarShadow.setVisibility(View.VISIBLE);
        initTitle(R.drawable.icon_aleft, "订单详情");
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        try {
            showLoading();
            OrderInfoBean orderInfoBean = (OrderInfoBean) getIntent().getSerializableExtra("orderInfo");
            if (orderInfoBean != null && orderInfoBean.getOrderInfo() != null) {
                OrderDesInfoBean orderInfo = orderInfoBean.getOrderInfo();
                textBuyDate.setText("下单日期：" + DateUtils.getFriendlyDate(orderInfo.getOrderDate()));
                textClientName.setText("客户姓名：" + orderInfo.getCustomerName());
                textClientPhone.setText("客户手机：" + orderInfo.getCustomerMobile());
                textUserName.setText("收件人：" + orderInfo.getReceiveName());
                textUserPhone.setText("联系电话：" + orderInfo.getReceiveMobile());
                orderId = orderInfo.getOrderId();
                if (orderInfo.getAddress() != null) {
                    String address = orderInfo.getAddress().replaceAll("\\|", "");
                    textAddress.setText("收货地址：" + address);
                }
                //产品信息
                String mImgUrl = getIntent().getStringExtra("imgUrl");
                String mDeviceName = getIntent().getStringExtra("deviceName");
                String mNormsidName = getIntent().getStringExtra("normsidName");
                LoaderImage.onLoadingImage(mImgUrl, imageDevideImg, R.drawable.img_loading);
                textDeviceName.setText(mDeviceName);
                textDeviceNormsid.setText("规格分类：" + mNormsidName);
                imageQrcode.setImageBitmap(BitmapUtil.createQRCode(orderInfoBean.getQrcode(), DisplayUtil.dip2px(this, 130)));
            }
            hideEmptyLayout();
        } catch (Exception e) {
            showNoNetWork();
        }
        //退出之前的页面
        MallActivityList.getInstance().exit();
    }

    @OnClick(R.id.btn_order_des)
    public void onClick() {
        //orderId
        if (mNoDoubleClick.check()) return;
        Intent intent = new Intent(this, SalesActivity.class);
        intent.putExtra("orderId", Integer.valueOf(orderId));
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
